package cn.tedu.sp04.Service;

import cn.tedu.sp1.Service.OrderService;
import cn.tedu.sp1.Web.Util.JsonResult;
import cn.tedu.sp1.entity.Item;
import cn.tedu.sp1.entity.Order;
import cn.tedu.sp1.entity.User;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.netflix.discovery.converters.Auto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient ;
    @Autowired
    private UserCilent userCilent ;
    @Override
    public Order getOrder(String orderId) {
         //TODO:远程调用商品，获取商品列表  执行流程Controller ， service ， mapper ， sql语句查数据库
        JsonResult<List<Item>> items = itemClient.getItems(orderId);
        //TODO:远程调用用户，获取用户列表
        JsonResult<User> users = userCilent.getUser(8);

        Order order = new Order();
        order.setId(orderId);

        log.info("获取订单完成id：" +orderId);
        order.setItems(items.getData());
        order.setUser(users.getData());
        // return null;
        return order ;
    }

    @Override
    public void addOrder(Order order) {
        log.info("添加订单:"+order);
        /*远程调用商品，减少库存   订单+1 ， 库存-1
        * 调用用户，增加用户积分*/
        itemClient.decraeseNumber(order.getItems());
        userCilent.addScore(212,1111);
    }
}
